package com.blacksheep.mtm.controller;

import com.blacksheep.mtm.dto.OrderDto;
import com.blacksheep.mtm.dto.front.OrderUpdateVo;
import com.blacksheep.mtm.pojo.common.DataTablesResult;
import com.blacksheep.mtm.pojo.common.Result;
import com.blacksheep.mtm.common.utils.ResultUtil;
import com.blacksheep.mtm.pojo.manager.TbOrder;
import com.blacksheep.mtm.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author
 */
@RestController
@Api(description= "订单管理")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @RequestMapping(value = "/order/list",method = RequestMethod.GET)
    @ApiOperation(value = "获取订单列表")
    public DataTablesResult getOrderList(int draw, int start, int length, String searchKey,int[] state,
                                         String minDate, String maxDate,
                                         @RequestParam("search[value]") String search,
                                         @RequestParam("order[0][column]") int orderCol,
                                         @RequestParam("order[0][dir]") String orderDir){

        //获取客户端需要排序的列
        String[] cols = {"checkbox","order_id", "payment_type","shipping_id", "cc_id", "dispenser_id", "dispatcher_id", "reserve_time", "create_time", "end_time","status"};
        String orderColumn = cols[orderCol];
        //默认排序列
        if(orderColumn == null) {
            orderColumn = "create_time";
        }
        //获取排序方式 默认为desc(asc)
        if(orderDir == null) {
            orderDir = "desc";
        }
        if (!StringUtils.isBlank(search)){
            searchKey =search;
        }
        DataTablesResult result= orderService.getOrderList(draw,start,length,state,searchKey,minDate,maxDate,orderColumn,orderDir);
        return result;
    }

    @RequestMapping(value = "/order/uncomfired/list",method = RequestMethod.GET)
    @ApiOperation(value = "根据状态获取所有订单列表")
    public DataTablesResult getUncomfiredOrderList(int draw, int start, int length, String searchKey,int[] state,@RequestParam(value = "isPrint",required = false) Integer isPrint,
                                         String minDate, String maxDate,
                                         @RequestParam("search[value]") String search,
                                         @RequestParam("order[0][column]") int orderCol,
                                         @RequestParam("order[0][dir]") String orderDir){

        //获取客户端需要排序的列
        String[] cols = {"checkbox","order_id", "s.receiver_name","","receiver_phone","create_time", "reserve_time", "receiver_address", "insurance", "insurance_type", "payment_type", "delivery_name","member_id","freezing","status"};
        String orderColumn = cols[orderCol];
        //默认排序列
        if(orderColumn == null) {
            orderColumn = "create_time";
        }
        //获取排序方式 默认为desc(asc)
        if(orderDir == null) {
            orderDir = "desc";
        }
        if (!StringUtils.isBlank(search)){
            searchKey =search;
        }
        if (isPrint ==null){
            isPrint = -1;
        }
        DataTablesResult result= orderService.getUncomfiredOrderList(draw,start,length,state,isPrint,searchKey,minDate,maxDate,orderColumn,orderDir);
        return result;
    }
    @RequestMapping(value = "/order/finish/list",method = RequestMethod.GET)
    @ApiOperation(value = "获取完成订单列表")
    public DataTablesResult getFinishOrderList(int draw, int start, int length, String searchKey,@RequestParam(value = "isPrint",required = false) Integer isPrint,
                                                   String minDate, String maxDate,
                                                   @RequestParam("search[value]") String search,
                                                   @RequestParam("order[0][column]") int orderCol,
                                                   @RequestParam("order[0][dir]") String orderDir){

        //获取客户端需要排序的列
        String[] cols = {"checkbox","order_id", "s.receiver_name","receiver_phone","","create_time", "closeTime","consignTime","reserveTime","receiver_address", "delivery_name","cost","member_id"};
        String orderColumn = cols[orderCol];
        //默认排序列
        if(orderColumn == null) {
            orderColumn = "create_time";
        }
        //获取排序方式 默认为desc(asc)
        if(orderDir == null) {
            orderDir = "desc";
        }
        if (!StringUtils.isBlank(search)){
            searchKey =search;
        }
        if (isPrint ==null){
            isPrint = -1;
        }
        int[] state = {40};
        DataTablesResult result= orderService.getUncomfiredOrderList(draw,start,length,state,isPrint,searchKey,minDate,maxDate,orderColumn,orderDir);
        return result;
    }
    @RequestMapping(value = "/order/all/list",method = RequestMethod.GET)
    @ApiOperation(value = "获取完成订单列表")
    public DataTablesResult getAllOrderList(int draw, int start, int length, String searchKey,@RequestParam(value = "isPrint",required = false) Integer isPrint,
                                               String minDate, String maxDate,
                                               @RequestParam("search[value]") String search,
                                               @RequestParam("order[0][column]") int orderCol,
                                               @RequestParam("order[0][dir]") String orderDir){

        //获取客户端需要排序的列
        String[] cols = {"checkbox","order_id", "s.receiver_name","receiver_phone","create_time","reserveTime","receiver_address","insurance", "insurance_type","", "payment_type","delivery_name","status"};
        String orderColumn = cols[orderCol];
        //默认排序列
        if(orderColumn == null) {
            orderColumn = "create_time";
        }
        //获取排序方式 默认为desc(asc)
        if(orderDir == null) {
            orderDir = "desc";
        }
        if (!StringUtils.isBlank(search)){
            searchKey =search;
        }
        if (isPrint ==null){
            isPrint = -1;
        }
        DataTablesResult result= orderService.getUncomfiredOrderList(draw,start,length,null,isPrint,searchKey,minDate,maxDate,orderColumn,orderDir);
        return result;
    }


    @RequestMapping(value = "/order/cancel/list",method = RequestMethod.GET)
    @ApiOperation(value = "获取取消的订单列表")
    public DataTablesResult getCancelOrderList(int draw, int start, int length, String searchKey,@RequestParam(value = "isPrint",required = false) Integer isPrint,
                                            String minDate, String maxDate,
                                            @RequestParam("search[value]") String search,
                                            @RequestParam("order[0][column]") int orderCol,
                                            @RequestParam("order[0][dir]") String orderDir){

        //获取客户端需要排序的列
        String[] cols = {"checkbox","order_id", "s.receiver_name","receiver_phone","create_time","reserveTime","receiver_address","insurance", "insurance_type","", "payment_type","delivery_name","status"};
        String orderColumn = cols[orderCol];
        //默认排序列
        if(orderColumn == null) {
            orderColumn = "create_time";
        }
        //获取排序方式 默认为desc(asc)
        if(orderDir == null) {
            orderDir = "desc";
        }
        if (!StringUtils.isBlank(search)){
            searchKey =search;
        }
        if (isPrint ==null){
            isPrint = -1;
        }
        int[] state = {40};
        DataTablesResult result= orderService.getCancelOrderList(draw,start,length,state,isPrint,searchKey,minDate,maxDate,orderColumn,orderDir);
        return result;
    }
//
//    @RequestMapping(value = "/order/del/{ids}",method = RequestMethod.DELETE)
//    @ApiOperation(value = "删除订单")
//    public Result<Object> getUserInfo(@PathVariable String[] ids){
//
//        for(String id:ids){
//            orderService.deleteOrder(id);
//        }
//        return new ResultUtil<Object>().setData(null);
//    }
    @RequestMapping(value = "/order/add",method = RequestMethod.POST)
    @ApiOperation(value = "新增订单")
    public Result<TbOrder> insertOrder(OrderDto orderDto,Integer wxOrderId){
        TbOrder order = orderService.insertOrder(orderDto,wxOrderId);
        return new ResultUtil<TbOrder>().setData(order);
    }

    @RequestMapping(value = "/order/{id}/cc",method = RequestMethod.POST)
    @ApiOperation(value = "已联系订单,并更改状态")
    public Result<TbOrder> updateOrderCC(@PathVariable("id")String[] id){
        orderService.updateOrderCc(id);
        return new ResultUtil<TbOrder>().setData(null);
    }

    @RequestMapping(value = "/order/{id}/dispenser",method = RequestMethod.POST)
    @ApiOperation(value = "订单已配药,并更改状态")
    public Result<TbOrder> updateOrderDispenser(@PathVariable("id")String[] id){
        orderService.updateOrderDispenser(id);
        return new ResultUtil<TbOrder>().setData(null);
    }

    @RequestMapping(value = "/order/{id}/review",method = RequestMethod.POST)
    @ApiOperation(value = "审核订单")
    public Result<TbOrder> reviewOrder(@PathVariable("id")String id,String comment,int state){
        int result = orderService.reviewOrder(id,state,comment);
        return new ResultUtil<TbOrder>().setData(null);
    }

    @RequestMapping(value = "/order/{id}/dispatcher",method = RequestMethod.POST)
    @ApiOperation(value = "订单已分配配送员,并更改状态")
    public Result<TbOrder> updateOrderDispatcher(@PathVariable("id")String id[],Long mid){
        orderService.updateOrderDispatcher(id,mid);
        return new ResultUtil<TbOrder>().setData(null);
    }


    @RequestMapping(value = "/order/{id}/reserve",method = RequestMethod.POST)
    @ApiOperation(value = "分配配送时间")
    public Result<TbOrder> updateOrderReserveTime(@PathVariable("id")String id,String reserveTime){
        orderService.updateOrderReserveTime(id,reserveTime);
        return new ResultUtil<TbOrder>().setData(null);
    }

    @RequestMapping(value = "/order/{id}/cancel",method = RequestMethod.POST)
    @ApiOperation(value = "取消订单")
    public Result<TbOrder> cancelOrder(@PathVariable("id")String id,String content){
        orderService.cancelOrder(id,content);
        return new ResultUtil<TbOrder>().setData(null);
    }


    @RequestMapping(value = "/order/{id}/update",method = RequestMethod.POST)
    @ApiOperation(value = "订单修改")
    public Result<TbOrder> updateOrder(@PathVariable("id")String id,OrderUpdateVo vo){
        orderService.updateOrder(id,vo);
        return new ResultUtil<TbOrder>().setData(null);
    }

    @RequestMapping(value = "/order/print/update",method = RequestMethod.POST)
    @ApiOperation(value = "订单修改")
    public Result<TbOrder> updateOrderPrintState(String[] orders,int state){
        orderService.updateOrderPrintState(orders,state);
        return new ResultUtil<TbOrder>().setData(null);
    }

    @RequestMapping(value = "/order/download",method = RequestMethod.GET)
    @ApiOperation(value = "导出")
    public void downloadExcel(String minDate, String maxDate, HttpServletResponse response) throws IOException {
        int[] state = {40};
        orderService.downloadExcel(state,minDate,maxDate,response);
    }

    @RequestMapping(value = "/order/all/download",method = RequestMethod.GET)
    @ApiOperation(value = "导出所有订单")
    public void downloadAllOrderExcel(String minDate, String maxDate, HttpServletResponse response) throws IOException {
        orderService.downloadExcel(null,minDate,maxDate,response);
    }

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }
}
